package com.zpark.controller;

import com.zpark.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.zpark.controller
 * @ClassName: UserController
 * @Description:
 * @author: RZS
 * @date: 2024/4/17  11:39
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // 伪造数据
    List<User> lists = new ArrayList<>();

    public UserController(){
        lists.add(new User(1, "大锤", "123456"));
        lists.add(new User(2, "张三", "123456"));
        lists.add(new User(3, "李四", "123456"));
        lists.add(new User(4, "王五", "123456"));
        lists.add(new User(5, "赵六", "123456"));
        lists.add(new User(6, "钱七", "123456"));
        lists.add(new User(7, "锤锤", "123456"));
    }

    @GetMapping("/all")
    public List<User> getAll(){
        return lists;
    }

    @GetMapping("/get_id/{id}")
    public User getId(@PathVariable Integer id){
        // 查询id为1的数据
        for (User user : lists) {
            if (user.getId().equals(id)) {
                return user;
            }
        }

        return null;
    };

    @PostMapping("/update")
    public void update(@RequestBody User user){
        for (User list : lists) {
            // 根据id修改数据
            if (list.getId().equals(user.getId())) {
                list.setUname(user.getUname());
                list.setPwd(user.getPwd());
            }
        }
    }

    @PostMapping("/insert")
    public void insert(@RequestBody User user){
        lists.add(user);
    }

    @GetMapping("/del/{id}")
    public void delete(@PathVariable Integer id){
        // 先查询对象
        for (User user : lists) {
            if (user.getId().equals(id)) {
                // 移除元素
                lists.remove(user);
            }
        }
    }
}
